package it.gat.webservice.resource.impl;

import java.sql.SQLException;

import org.restlet.resource.Delete;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ServerResource;

import it.gat.dao.SedeDAO;
import it.gat.model.Sede;
import it.gat.util.dao.DAOFactory;
import it.gat.webservice.utils.Impostazioni;

public class SedeResource extends ServerResource{
	
	@Post
	public Sede insert(Sede sede) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		SedeDAO sedeDAO = daoFactory.getSedeDAO();
		Sede nuovaSede = null;
		try {
			nuovaSede = sedeDAO.insert(sede, Integer.parseInt(getRequestAttributes().get("id").toString()));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return nuovaSede;
	}

	@Get
	public Sede retriveById() {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		SedeDAO sedeDAO = daoFactory.getSedeDAO();
		Sede sede;
		try {
			sede = sedeDAO.retriveById(Integer.parseInt(getRequestAttributes().get("id").toString()));
		} catch (NumberFormatException e) {
			return null;
		} catch (SQLException e) {
			return null;
		}
		return sede;
	}

	@Put
	public void update(Sede sede) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		SedeDAO sedeDAO = daoFactory.getSedeDAO();
		try {
			sedeDAO.update(sede);
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

	@Delete
	public void delete(Sede sede) {
		DAOFactory daoFactory = DAOFactory.getDAOFactory(Integer.parseInt(Impostazioni.getProperty("DB_TYPE")));
		SedeDAO sedeDAO = daoFactory.getSedeDAO();
		try {
			sedeDAO.delete(Integer.parseInt(getRequestAttributes().get("id").toString()));
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
